package org.javacore.Utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import java.security.MessageDigest;

public class EncryptUtils {

	private final static String ALGORITHM = "DES";

	private final static String KEY = "gzihk.cn"; // 必须8位

	/**
	 * 功能:加密
	 * 作者:曾俊华
	 * 时间:2013-12-17
	 */
	public static String encryptData(String info) throws Exception {
		if (info != null && !"".equals(info)) {
			SecretKey deskey = new javax.crypto.spec.SecretKeySpec(
					KEY.getBytes(), ALGORITHM);
			Cipher c1 = Cipher.getInstance(ALGORITHM);
			c1.init(Cipher.ENCRYPT_MODE, deskey);
			byte[] cipherByte = c1.doFinal(info.getBytes());
			return byte2hex(cipherByte).toUpperCase();
		}
		return null;
	}

	/**
	 * 功能:解密
	 * 作者:曾俊华
	 * 时间:2013-12-17
	 */
	public static String decryptData(String info) throws Exception {
		if (info != null && !"".equals(info)) {
			SecretKey deskey = new javax.crypto.spec.SecretKeySpec(
					KEY.getBytes(), ALGORITHM);
			Cipher c1 = Cipher.getInstance(ALGORITHM);
			c1.init(Cipher.DECRYPT_MODE, deskey);
			byte[] cipherByte = c1.doFinal(hex2byte(info));
			return new String(cipherByte);
		}
		return null;
	}

	/**
	 * 功能:16进制->byte
	 * 作者:曾俊华
	 * 时间:2013-12-17
	 */
	public static byte[] hex2byte(String hex) {
		int length = hex.length() / 2;
		byte[] ret = new byte[length];
		byte[] tmp = hex.getBytes();
		for (int i = 0; i < length; i++) {
			ret[i] = uniteBytes(tmp[i * 2], tmp[i * 2 + 1]);
		}
		return ret;
	}

	/**
	 * 功能:将两个ASCII字符合成一个字节； 如："EF"--> 0xEF
	 * 作者:曾俊华
	 * 时间:2013-12-17
	 */
	public static byte uniteBytes(byte src0, byte src1) {
		byte _b0 = Byte.decode("0x" + new String(new byte[] { src0 }))
				.byteValue();
		_b0 = (byte) (_b0 << 4);
		byte _b1 = Byte.decode("0x" + new String(new byte[] { src1 }))
				.byteValue();
		byte ret = (byte) (_b0 ^ _b1);
		return ret;
	}


	/**
	 * 功能:byte->16进制
	 * 作者:曾俊华
	 * 时间:2013-12-17
	 */
	public static String byte2hex(byte[] b) {
		String hs = "";
		String stmp = "";
		for (int n = 0; n < b.length; n++) {
			stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
			if (stmp.length() == 1) {
				hs = hs + "0" + stmp;
			} else {
				hs = hs + stmp;
			}
		}
		return hs.toUpperCase();
	}

    /***
     * MD5加密 生成32位md5码
     * @param inStr 待加密字符串
     * @return 返回32位md5码
     */
    public static String md5Encode(String inStr) throws Exception {
        MessageDigest md5 = null;
        try {
            md5 = MessageDigest.getInstance("MD5");
        } catch (Exception e) {
            System.out.println(e.toString());
            e.printStackTrace();
            return "";
        }

        byte[] byteArray = inStr.getBytes("UTF-8");
        byte[] md5Bytes = md5.digest(byteArray);
        StringBuffer hexValue = new StringBuffer();
        for (int i = 0; i < md5Bytes.length; i++) {
            int val = ((int) md5Bytes[i]) & 0xff;
            if (val < 16) {
                hexValue.append("0");
            }
            hexValue.append(Integer.toHexString(val));
        }
        return hexValue.toString();
    }
}
